<?php
class Form_Register extends Zend_Form {

public function init()
{
    parent::init();
    $this->setAction('/index/register');
    $this->setMethod('post');
    $this->setAttrib('class', 'register');

    // Fields are listed in the order they are presented on the Register foem /index/register
    $email = new Form_Element_Email('email');
    $email->setAttrib('required', true);
    $email->addValidator('Db_NoRecordExists', true, array('users', 'email'));
    $this->addElement($email);

    $password = new Form_Element_Password('password');
    $password->setAttrib('required', true);
    $this->addElement($password);

    $passwordApprove = new Zend_Form_Element_Password('password_approve', array(
        'required'    => true,
        'label'       => 'Password again:',
        'maxlength'   => '30',
        'validators'  => array(array(new Validate_EqualInputs('password'), true)),
    ));
    $this->addElement($passwordApprove);

    $name = new Form_Element_Name('name');
    $name->setAttrib('required', true);
    $this->addElement($name);

    $location = new Form_Element_Location('location');
    $location->setAttrib('required', true);
    $this->addElement($location);

    $sex = new Zend_Form_Element_Radio('sex', array(
        'label'       => 'Sex:',
        'multiOptions'=> array('m' => 'Male', 'f' => 'Female'),
        'validators'  => array(array('InArray', true, array(array('m', 'f'), true)))
    ));
    $sex->setSeparator(' ');
    $this->addElement($sex);

//        $dateBirth = new Zend_Form_Element_Text('date_birth', array(
//            'label'       => 'Date of birth:',
//            'maxlength'   => '10',
//            'validators'  => array(array('Date', true, array('dd.MM.yyyy'))),
//            'filters'     => array('StringTrim'),
//        ));
//        $dateBirth->clearDecorators();
//        $dateBirth
//            ->addDecorator('ViewHelper')
////            ->addDecorator('Calendar')
//            ->addDecorator('Errors')
//            ->addDecorator('HtmlTag', array('tag' => 'dd'))
//            ->addDecorator('Label', array('tag' => 'dt'));
//        $this->addElement($dateBirth);

    $age = new Zend_Form_Element_Select('age', array(
        'label'       => 'Age:',
        'multiOptions'=> array('', '11 - 20', '21 - 30', '31 - 40'),
        'filters'     => array('Int'),
    ));
    $this->addElement($age);

    $about = new Zend_Form_Element_Textarea('about', array(
        'label'       => 'A few words about:',
        'rows'        => '5',
        'cols'        => '45',
        'validators'  => array(
            array('StringLength', true, array(0, 5000))
         ),
        'filters'     => array('StringTrim'),
    ));
    $this->addElement($about);

    $captcha = new Zend_Form_Element_Captcha('captcha', array(
        'label' => "Please, enter symbols you see above:",
        'required' => true,
        'captcha' => array(
            'captcha'   => 'Figlet',
            'name' => 'foo',
            'wordLen'   => 4,
        ),
    ));
    $this->addElement($captcha);

    // Custom validator message
    $validatorNotEmpty = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::INTEGER);
    $validatorNotEmpty->setMessages(array(
        Zend_Validate_NotEmpty::IS_EMPTY  => 'You should accept the rules'));

    $agreeRules = new Zend_Form_Element_Checkbox('agreeRules', array(
        'label'       => 'I accept the rules:',
        'setRegisterInArrayValidator' => false,
        'allowEmpty' => false,
        'filters'     => array('Int'),
        'validators'  => array($validatorNotEmpty),
    ));
    $this->addElement($agreeRules);

    $submit = new Zend_Form_Element_Submit('submit', array(
        'label'       => 'Register',
    ));
    $submit->setDecorators(array('ViewHelper'));
    $this->addElement($submit);

    $reset = new Zend_Form_Element_Reset('reset', array(
        'label'       => 'Clean',
    ));

    $reset->setDecorators(array('ViewHelper'));
    $this->addElement($reset);

    //Display in fieldsets elements with the same legend
    $this->addDisplayGroup(
        array('email', 'password', 'password_approve'), 'registerDataGroup',
        array(
            'legend' => 'Authorised data'
        )
    );

    $this->addDisplayGroup(
        array('name', 'last_name', 'location','sex', 'date_birth', 'age', 'about' ), 'privateDataGroup',
        array(
            'legend' => 'Personal information'
        )
    );

    $this->addDisplayGroup(
        array('captcha'), 'captchaGroup',
        array(
            'legend' => 'Are you a human?!'
        )
    );

    $this->addDisplayGroup(
        array('agreeRules', 'submit', 'reset'), 'buttonsGroup'
    );

}
}
